//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.PrintBTConnectController');
	com.yonyou.ncmm_pac_pda.PrintBTConnectController = function() {
		com.yonyou.ncmm_pac_pda.PrintBTConnectController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//    
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$evaljs(js) {
		eval(js)
	}
	var result_json;
	var address;
	var select_No = -1;

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$button2_onclick(sender, args) {
		$ctx.push(JSON.parse("{}"));
		if(select_No != -1) {
			$service.call("DXYBTPrinterConnectService.cancelConnect", {
				"param": address
			}, "true");
			select_No = -1;
			$id("label1").set("value", "未连接打印设备");
		}
		var res = $service.call("DXYBTPrinterConnectService.startDis", {}, "true");
		//$alert(res);
		$js.showLoadingBar();
		while($service.call("DXYBTPrinterConnectService.getStatus", {}, "true") == "false") {}
		$js.hideLoadingBar();
		var result = $service.call("DXYBTPrinterConnectService.getList", {}, "true");
		//$alert(result);
		result_json = JSON.parse(result);
		$ctx.push(result_json);
		$cache.write("BlueToothList", result_json);
	}
	//var material={"material_pk":"GT123456","material_name":"钢铁","material_spec":"优等品","material_model":"GT654321"};
	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$button3_onclick(sender, args) {
		if(select_No == -1) {
			$alert("未连接打印机，无法进行打印");
		} else {
			$view.open({
				"viewid": "com.yonyou.ncmm_pac_pda.PoArrivalOrderSearch", //目标页面（首字母大写）全名
				"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
				"callback": "myCallBack()" //关闭viewid页面后，执行的回调方法
			});
		}
	}

	function myCallBack() {

	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$listItemCLick(sender, args) {
		var index = $id("listviewdefine0").get("rowindex");
		if(select_No == -1) {
			$toast("当前选中的打印机为：" + result_json["bluetooth"][index]["bt_name"]);
			$js.showLoadingBar();
			address = result_json["bluetooth"][index]["bt_addr"];
			$service.call("DXYBTPrinterConnectService.getConnect", {
				"param": address
			}, "true");
			var begindate = new Date();
			var begintime = parseFloat(begindate.getTime());
			var curtime = parseFloat(new Date().getTime());
			var diffTime = curtime - begintime;
			while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected" && (diffTime / parseFloat(1000)) < 30) {
				diffTime = parseFloat(new Date().getTime()) - begintime;
			}
			if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected") {
				$js.hideLoadingBar();
				$alert("连接超时...");
				return;
			}
			$js.hideLoadingBar();
			select_No = index;
			result_json["bluetooth"][index]["bt_select"] = "已连接";
			$id("label1").set("value", "已连接" + result_json["bluetooth"][index]["bt_name"]);
		} else if(index == select_No) {
			if($confirm("是否断开与" + result_json["bluetooth"][index]["bt_name"] + "的连接")) {
				$js.showLoadingBar();
				$service.call("DXYBTPrinterConnectService.cancelConnect", {
					"param": address
				}, "true");
				var begindate = new Date();
				var begintime = parseFloat(begindate.getTime());
				var curtime = parseFloat(new Date().getTime());
				var diffTime = curtime - begintime;
				while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "connected" && (diffTime / parseFloat(1000)) < 30) {
					diffTime = parseFloat(new Date().getTime()) - begintime;
				}
				if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "connected") {
					$js.hideLoadingBar();
					$alert("断开连接超时...");
					return;
				}
				$js.hideLoadingBar();
				result_json["bluetooth"][select_No]["bt_select"] = "未连接";
				$id("label1").set("value", "未连接打印设备");
				select_No = -1;
				$toast("已断开与" + result_json["bluetooth"][index]["bt_name"] + "的连接");
			}
		} else {
			$service.call("DXYBTPrinterConnectService.cancelConnect", {
				"param": address
			}, "true");
			result_json["bluetooth"][select_No]["bt_select"] = "未连接";
			$toast("当前选中的打印机为：" + result_json["bluetooth"][index]["bt_name"]);
			address = result_json["bluetooth"][index]["bt_addr"];
			$service.call("DXYBTPrinterConnectService.getConnect", {
				"param": address
			}, "true");
			var begindate = new Date();
			var begintime = parseFloat(begindate.getTime());
			var curtime = parseFloat(new Date().getTime());
			var diffTime = curtime - begintime;
			while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected" && (diffTime / parseFloat(1000)) < 30) {
				diffTime = parseFloat(new Date().getTime()) - begintime;
			}
			if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected") {
				$js.hideLoadingBar();
				$alert("连接超时...");
				return;
			}
			$js.hideLoadingBar();
			select_No = index;
			result_json["bluetooth"][index]["bt_select"] = "已连接";
			$id("label1").set("value", "已连接" + result_json["bluetooth"][index]["bt_name"]);
		}
		$ctx.push(result_json);
	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$button0_onclick(sender, args) {
		$view.close({
			"resultCode": "15" //当resultCode为15时，可以响应父页面的callback
		});
	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$viewonload(sender, args) {
		result_json = $cache.read("BlueToothList");
		if(result_json != undefined && result_json != "") {
			$ctx.push(result_json);
			result_json = $stringToJSON(result_json);
		}
	}
	com.yonyou.ncmm_pac_pda.PrintBTConnectController.prototype = {
		viewonload: com$yonyou$ncmm_pac_pda$PrintBTConnectController$viewonload,
		button0_onclick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$button0_onclick,
		listItemCLick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$listItemCLick,
		button3_onclick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$button3_onclick,
		button2_onclick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$button2_onclick,
		initialize: com$yonyou$ncmm_pac_pda$PrintBTConnectController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$PrintBTConnectController$evaljs
	};
	com.yonyou.ncmm_pac_pda.PrintBTConnectController.registerClass('com.yonyou.ncmm_pac_pda.PrintBTConnectController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}
//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.PrintBTConnectController');
	com.yonyou.ncmm_pac_pda.PrintBTConnectController = function() {
		com.yonyou.ncmm_pac_pda.PrintBTConnectController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//    
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$evaljs(js) {
		eval(js)
	}
	var result_json;
	var address;
	var select_No = -1;

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$button2_onclick(sender, args) {
		$ctx.push(JSON.parse("{}"));
		if(select_No != -1) {
			$service.call("DXYBTPrinterConnectService.cancelConnect", {
				"param": address
			}, "true");
			select_No = -1;
			$id("label1").set("value", "未连接打印设备");
		}
		var res = $service.call("DXYBTPrinterConnectService.startDis", {}, "true");
		//$alert(res);
		$js.showLoadingBar();
		while($service.call("DXYBTPrinterConnectService.getStatus", {}, "true") == "false") {}
		$js.hideLoadingBar();
		var result = $service.call("DXYBTPrinterConnectService.getList", {}, "true");
		//$alert(result);
		result_json = JSON.parse(result);
		$ctx.push(result_json);
		$cache.write("BlueToothList", result_json);
	}
	//var material={"material_pk":"GT123456","material_name":"钢铁","material_spec":"优等品","material_model":"GT654321"};
	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$button3_onclick(sender, args) {
		if(select_No == -1) {
			$alert("未连接打印机，无法进行打印");
		} else {
			$view.open({
				"viewid": "com.yonyou.ncmm_pac_pda.PoArrivalOrderSearch", //目标页面（首字母大写）全名
				"isKeep": "true", //打开新页面的同时是否保留当前页面，true为保留，false为不保留
				"callback": "myCallBack()" //关闭viewid页面后，执行的回调方法
			});
		}
	}

	function myCallBack() {

	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$listItemCLick(sender, args) {
		var index = $id("listviewdefine0").get("rowindex");
		if(select_No == -1) {
			$toast("当前选中的打印机为：" + result_json["bluetooth"][index]["bt_name"]);
			$js.showLoadingBar();
			address = result_json["bluetooth"][index]["bt_addr"];
			$service.call("DXYBTPrinterConnectService.getConnect", {
				"param": address
			}, "true");
			var begindate = new Date();
			var begintime = parseFloat(begindate.getTime());
			var curtime = parseFloat(new Date().getTime());
			var diffTime = curtime - begintime;
			while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected" && (diffTime / parseFloat(1000)) < 30) {
				diffTime = parseFloat(new Date().getTime()) - begintime;
			}
			if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected") {
				$js.hideLoadingBar();
				$alert("连接超时...");
				return;
			}
			$js.hideLoadingBar();
			select_No = index;
			result_json["bluetooth"][index]["bt_select"] = "已连接";
			$id("label1").set("value", "已连接" + result_json["bluetooth"][index]["bt_name"]);
		} else if(index == select_No) {
			if($confirm("是否断开与" + result_json["bluetooth"][index]["bt_name"] + "的连接")) {
				$js.showLoadingBar();
				$service.call("DXYBTPrinterConnectService.cancelConnect", {
					"param": address
				}, "true");
				var begindate = new Date();
				var begintime = parseFloat(begindate.getTime());
				var curtime = parseFloat(new Date().getTime());
				var diffTime = curtime - begintime;
				while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "connected" && (diffTime / parseFloat(1000)) < 30) {
					diffTime = parseFloat(new Date().getTime()) - begintime;
				}
				if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "connected") {
					$js.hideLoadingBar();
					$alert("断开连接超时...");
					return;
				}
				$js.hideLoadingBar();
				result_json["bluetooth"][select_No]["bt_select"] = "未连接";
				$id("label1").set("value", "未连接打印设备");
				select_No = -1;
				$toast("已断开与" + result_json["bluetooth"][index]["bt_name"] + "的连接");
			}
		} else {
			$service.call("DXYBTPrinterConnectService.cancelConnect", {
				"param": address
			}, "true");
			result_json["bluetooth"][select_No]["bt_select"] = "未连接";
			$toast("当前选中的打印机为：" + result_json["bluetooth"][index]["bt_name"]);
			address = result_json["bluetooth"][index]["bt_addr"];
			$service.call("DXYBTPrinterConnectService.getConnect", {
				"param": address
			}, "true");
			var begindate = new Date();
			var begintime = parseFloat(begindate.getTime());
			var curtime = parseFloat(new Date().getTime());
			var diffTime = curtime - begintime;
			while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected" && (diffTime / parseFloat(1000)) < 30) {
				diffTime = parseFloat(new Date().getTime()) - begintime;
			}
			if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected") {
				$js.hideLoadingBar();
				$alert("连接超时...");
				return;
			}
			$js.hideLoadingBar();
			select_No = index;
			result_json["bluetooth"][index]["bt_select"] = "已连接";
			$id("label1").set("value", "已连接" + result_json["bluetooth"][index]["bt_name"]);
		}
		$ctx.push(result_json);
	}
	
	function disconnectFunc() {
		var index = $id("listviewdefine0").get("rowindex");
		$service.call("DXYBTPrinterConnectService.cancelConnect", {
			"param": address
		}, "true");
		var begindate = new Date();
		var begintime = parseFloat(begindate.getTime());
		var curtime = parseFloat(new Date().getTime());
		var diffTime = curtime - begintime;
		while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "connected" && (diffTime / parseFloat(1000)) < 30) {
			diffTime = parseFloat(new Date().getTime()) - begintime;
		}
		if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "connected") {
			return true;
		}
		result_json["bluetooth"][select_No]["bt_select"] = "未连接";
		$id("label1").set("value", "未连接打印设备");
		select_No = -1;
		return false;
	}

	function connectFunc() {
		var index = $id("listviewdefine0").get("rowindex");
		address = result_json["bluetooth"][index]["bt_addr"];
		$service.call("DXYBTPrinterConnectService.getConnect", {
			"param": address
		}, "true");
		var begindate = new Date();
		var begintime = parseFloat(begindate.getTime());
		var curtime = parseFloat(new Date().getTime());
		var diffTime = curtime - begintime;
		while($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected" && (diffTime / parseFloat(1000)) < 30) {
			diffTime = parseFloat(new Date().getTime()) - begintime;
		}
		if($service.call("DXYBTPrinterConnectService.getConnectStatus", {}, "true") == "unconnected") {
			return true;
		}
		select_No = index;
		result_json["bluetooth"][index]["bt_select"] = "已连接";
		$id("label1").set("value", "已连接" + result_json["bluetooth"][index]["bt_name"]);
		return false;
	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$button0_onclick(sender, args) {
		$view.close({
			"resultCode": "15" //当resultCode为15时，可以响应父页面的callback
		});
	}

	function com$yonyou$ncmm_pac_pda$PrintBTConnectController$viewonload(sender, args) {
		result_json = $cache.read("BlueToothList");
		if(result_json != undefined && result_json != "") {
			$ctx.push(result_json);
			result_json = $stringToJSON(result_json);
		}
	}
	com.yonyou.ncmm_pac_pda.PrintBTConnectController.prototype = {
		viewonload: com$yonyou$ncmm_pac_pda$PrintBTConnectController$viewonload,
		button0_onclick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$button0_onclick,
		listItemCLick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$listItemCLick,
		button3_onclick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$button3_onclick,
		button2_onclick: com$yonyou$ncmm_pac_pda$PrintBTConnectController$button2_onclick,
		initialize: com$yonyou$ncmm_pac_pda$PrintBTConnectController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$PrintBTConnectController$evaljs
	};
	com.yonyou.ncmm_pac_pda.PrintBTConnectController.registerClass('com.yonyou.ncmm_pac_pda.PrintBTConnectController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}